const mysql = require('mysql'); // mysql模块
const { v4: uuid } = require('uuid'); // UUID

const publicDao = require('dao/public-dao');

const tableName = 'comment'; // 表格名称
const service = {};

/**
 * 分页列表
 * @param {Object} params 参数
 * @param {Function} callback 回调
 */
service.getPageList = (params, callback) => {
  // 查询条件
  let whereStr = '';
      whereStr += params.flash_id ? 'flash_id=' + mysql.escape(params.flash_id) + ' AND ' : '';
      whereStr += params.audit_state ? 'audit_state=' + mysql.escape(params.audit_state) + ' AND ' : '';
  if (whereStr !== '') {
    whereStr = ' WHERE ' + whereStr.slice(0, whereStr.length - 5);
  }
  // 排序条件
  let orderByStr = ' ORDER BY time DESC';
  // dao
  publicDao.getPageList(tableName, params, whereStr, orderByStr, callback);
}

/**
 * 新增
 * @param {Object} params 参数
 * @param {Function} callback 回调
 */
service.add = (params, callback) => {
  // 设置默认值
  params.id = uuid(); // 生成UUID
  params.time = new Date(); // 修改时间
  // params.audit_state = 1; // 状态设置为待审核
  params.audit_state = 2; // 暂时设为-审核通过
  // dao
  publicDao.add(tableName, params, callback);
}

/**
 * 删除
 * @param {Array} ids id列表
 * @param {Function} callback 回调
 */
service.remove = (ids, callback) => {
  publicDao.remove(tableName, ids, callback);
}

/**
 * 审核
 * @param {Object} params 参数
 * @param {Function} callback 回调
 */
service.audit = (params, callback) => {
  publicDao.audit(tableName, params, callback);
}

module.exports = service;
